package rs.webnet.android.kamere;

import java.util.ArrayList;
import java.util.List;

import rs.webnet.android.kamere.data.DataFetcher;
import rs.webnet.android.kamere.data.Point;
import rs.webnet.android.kamere.util.FileResource;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class ReportsMapActivity extends MapActivity {
	MapView mapView;
	private MyLocationOverlay selfLocation;
	private MapController mapController;
	public LocationManager loc;
	private ArrayList<Point> odroni;
	private ArrayList<Point> info;
	private ArrayList<Point> radovi;
	private ArrayList<Point> zabranaTeretni;
	private ArrayList<Point> zabrana;
	private ArrayList<Point> carina;
	private ArrayList<Point> opasnost;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.camerasmap_layout);

		odroni = new ArrayList<Point>();
		info = new ArrayList<Point>();
		radovi = new ArrayList<Point>();
		zabranaTeretni = new ArrayList<Point>();
		zabrana = new ArrayList<Point>();
		carina = new ArrayList<Point>();
		opasnost = new ArrayList<Point>();

		for (Point p : DataFetcher.roadReport) {
			Log.d("d",""+p.getImage());
			if (p.getImage().contains("odron")) {
				odroni.add(p);
			} else if (p.getImage().contains("info")) {
				info.add(p);
			} else if (p.getImage().contains("radovi")) {
				radovi.add(p);
			} else if (p.getImage().contains("zabrana-teretni")) {
				zabranaTeretni.add(p);
			} else if (p.getImage().contains("zabrana")) {
				zabrana.add(p);
			}
		}

		Log.d("d",""+odroni.size()+" "+radovi.size());
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		mapController = mapView.getController();
		mapController.setZoom(15);

		List<Overlay> list = mapView.getOverlays();
		selfLocation = new MyLocationOverlay(this, mapView);
		selfLocation.enableMyLocation();
		selfLocation.enableCompass();
		selfLocation.runOnFirstFix(new Runnable() {
			public void run() {
				mapController.animateTo(selfLocation.getMyLocation());
			}
		});
		list.add(selfLocation);
		this.setObjectsPoi(list);
	}

	@Override
	public void onPause() {
		super.onPause();
		selfLocation.disableMyLocation();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private GeoPoint getMyLocation() {
		/* Gleda u prefs da li da koristi GPS ili ne i setuje locationProvider */
		String locationProvider;
		if (PreferencesManager.getUseGps(getApplicationContext()) != true) {
			locationProvider = LocationManager.NETWORK_PROVIDER;
		} else {
			locationProvider = LocationManager.GPS_PROVIDER;
		}

		loc = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
		// loc.requestLocationUpdates(locationProvider, 0, 0, locationListener);
		Location location = loc.getLastKnownLocation(locationProvider);

		// TODO pravi problem kada ni jedna app nije koristila GPS pa je
		// getLastKnown null

		Double lat = location.getLatitude() * 1E6;
		Double lng = location.getLongitude() * 1E6;
		GeoPoint center = new GeoPoint(lat.intValue(), lng.intValue());
		return center;
	}

	private void setObjectsPoi(List<Overlay> list) {

		DefaultItemizedOverlay overlay = new DefaultItemizedOverlay(getResources().getDrawable(R.drawable.report_default));
		for (Point point : DataFetcher.roadReport) {
			Double lat = Double.parseDouble(point.getLat()) * 1E6;
			Double lng = Double.parseDouble(point.getLng()) * 1E6;
			OverlayItem item = new OverlayItem(new GeoPoint(lat.intValue(), lng.intValue()), "My Location", null);
			overlay.addItem(item);
		}
		list.add(overlay);
//		/*
//		 * Odroni
//		 */
//		OdroniItemizedOverlay odroniOverlay = new OdroniItemizedOverlay(getResources().getDrawable(R.drawable.odron));
//		for (Point point : odroni) {
//			Double lat = Double.parseDouble(point.getLat()) * 1E6;
//			Double lng = Double.parseDouble(point.getLng()) * 1E6;
//			OverlayItem item = new OverlayItem(new GeoPoint(lat.intValue(), lng.intValue()), "My Location", null);
//			odroniOverlay.addItem(item);
//		}
//		list.add(odroniOverlay);
//		/*
//		 * Info
//		 */
//		InfoItemizedOverlay infoOverlay = new InfoItemizedOverlay(getResources().getDrawable(R.drawable.info));
//		for (Point point : info) {
//			Double lat = Double.parseDouble(point.getLat()) * 1E6;
//			Double lng = Double.parseDouble(point.getLng()) * 1E6;
//			OverlayItem item = new OverlayItem(new GeoPoint(lat.intValue(), lng.intValue()), "My Location", null);
//			infoOverlay.addItem(item);
//		}
//		list.add(infoOverlay);
//		/*
//		 * Radovi
//		 */
//		RadoviItemizedOverlay radoviOverlay = new RadoviItemizedOverlay(getResources().getDrawable(R.drawable.radovi));
//		for (Point point : radovi) {
//			Double lat = Double.parseDouble(point.getLat()) * 1E6;
//			Double lng = Double.parseDouble(point.getLng()) * 1E6;
//			OverlayItem item = new OverlayItem(new GeoPoint(lat.intValue(), lng.intValue()), "My Location", null);
//			radoviOverlay.addItem(item);
//		}
//		list.add(radoviOverlay);
//		/*
//		 * Zabrana teretni
//		 */
//		ZabranaTeretniItemizedOverlay zabranaTeretniOverlay = new ZabranaTeretniItemizedOverlay(getResources().getDrawable(R.drawable.zabrana_teretna));
//		for (Point point : zabranaTeretni) {
//			Double lat = Double.parseDouble(point.getLat()) * 1E6;
//			Double lng = Double.parseDouble(point.getLng()) * 1E6;
//			OverlayItem item = new OverlayItem(new GeoPoint(lat.intValue(), lng.intValue()), "My Location", null);
//			zabranaTeretniOverlay.addItem(item);
//		}
//		list.add(zabranaTeretniOverlay);
//		/*
//		 * Zabrana
//		 */
//		ZabranaItemizedOverlay zabranaOverlay = new ZabranaItemizedOverlay(getResources().getDrawable(R.drawable.zabrana));
//		for (Point point : zabrana) {
//			Double lat = Double.parseDouble(point.getLat()) * 1E6;
//			Double lng = Double.parseDouble(point.getLng()) * 1E6;
//			OverlayItem item = new OverlayItem(new GeoPoint(lat.intValue(), lng.intValue()), "My Location", null);
//			zabranaOverlay.addItem(item);
//		}
//		list.add(zabranaOverlay);
	}

	
	
	
	class DefaultItemizedOverlay extends ItemizedOverlay {
		private List<OverlayItem> items;
		private Drawable marker;

		public DefaultItemizedOverlay(Drawable defaultMarker) {
			super(defaultMarker);
			items = new ArrayList();
			marker = defaultMarker;
		}

		@Override
		protected OverlayItem createItem(int index) {
			return (OverlayItem) items.get(index);
		}

		@Override
		public int size() {
			return items.size();
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		public void addItem(OverlayItem item) {
			items.add(item);
			populate();
		}

		@Override
		protected boolean onTap(int index) {
			Log.d("d",""+index);
			Intent i = new Intent(ReportsMapActivity.this, PointActivity.class);
			i.putExtra("title", DataFetcher.roadReport.get(index).getTitle());
			i.putExtra("description", DataFetcher.roadReport.get(index).getDescription());
			i.putExtra("image", DataFetcher.roadReport.get(index).getImage());
			startActivity(i);
			return true;
		}

	}
	
	class OdroniItemizedOverlay extends ItemizedOverlay {
		private List<OverlayItem> items;
		private Drawable marker;

		public OdroniItemizedOverlay(Drawable defaultMarker) {
			super(defaultMarker);
			items = new ArrayList();
			marker = defaultMarker;
		}

		@Override
		protected OverlayItem createItem(int index) {
			return (OverlayItem) items.get(index);
		}

		@Override
		public int size() {
			return items.size();
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		public void addItem(OverlayItem item) {
			items.add(item);
			populate();
		}

		@Override
		protected boolean onTap(int index) {
			Log.d("d","odroni"+index);
//			Intent i = new Intent(ReportsMapActivity.this, PointActivity.class);
//			i.putExtra("title", odroni.get(index).getTitle());
//			i.putExtra("description", odroni.get(index).getDescription());
//			i.putExtra("image", odroni.get(index).getImage());
//			startActivity(i);
			return true;
		}

	}
	class InfoItemizedOverlay extends ItemizedOverlay {
		private List<OverlayItem> items;
		private Drawable marker;

		public InfoItemizedOverlay(Drawable defaultMarker) {
			super(defaultMarker);
			items = new ArrayList();
			marker = defaultMarker;
		}

		@Override
		protected OverlayItem createItem(int index) {
			return (OverlayItem) items.get(index);
		}

		@Override
		public int size() {
			return items.size();
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		public void addItem(OverlayItem item) {
			items.add(item);
			populate();
		}

		@Override
		protected boolean onTap(int index) {
			Log.d("d","info"+index);
//			Intent i = new Intent(ReportsMapActivity.this, PointActivity.class);
//			i.putExtra("title", info.get(index).getTitle());
//			i.putExtra("description", info.get(index).getDescription());
//			i.putExtra("image", info.get(index).getImage());
//			startActivity(i);
			return true;
		}
	}
	class RadoviItemizedOverlay extends ItemizedOverlay {
		private List<OverlayItem> items;
		private Drawable marker;

		public RadoviItemizedOverlay(Drawable defaultMarker) {
			super(defaultMarker);
			items = new ArrayList();
			marker = defaultMarker;
		}

		@Override
		protected OverlayItem createItem(int index) {
			return (OverlayItem) items.get(index);
		}

		@Override
		public int size() {
			return items.size();
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		public void addItem(OverlayItem item) {
			items.add(item);
			populate();
		}

		@Override
		protected boolean onTap(int index) {
			Log.d("d","radovi"+index);
//			Intent i = new Intent(ReportsMapActivity.this, PointActivity.class);
//			i.putExtra("title", radovi.get(index).getTitle());
//			i.putExtra("description", radovi.get(index).getDescription());
//			i.putExtra("image", radovi.get(index).getImage());
//			startActivity(i);
			return true;
		}
	}
	class ZabranaTeretniItemizedOverlay extends ItemizedOverlay {
		private List<OverlayItem> items;
		private Drawable marker;

		public ZabranaTeretniItemizedOverlay(Drawable defaultMarker) {
			super(defaultMarker);
			items = new ArrayList();
			marker = defaultMarker;
		}

		@Override
		protected OverlayItem createItem(int index) {
			return (OverlayItem) items.get(index);
		}

		@Override
		public int size() {
			return items.size();
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		public void addItem(OverlayItem item) {
			items.add(item);
			populate();
		}

		@Override
		protected boolean onTap(int index) {
			Log.d("d","zabranaTeretni"+index);
//			Intent i = new Intent(ReportsMapActivity.this, PointActivity.class);
//			i.putExtra("title", zabranaTeretni.get(index).getTitle());
//			i.putExtra("description", zabranaTeretni.get(index).getDescription());
//			i.putExtra("image", zabranaTeretni.get(index).getImage());
//			startActivity(i);
			return true;
		}
	}
	class ZabranaItemizedOverlay extends ItemizedOverlay {
		private List<OverlayItem> items;
		private Drawable marker;

		public ZabranaItemizedOverlay(Drawable defaultMarker) {
			super(defaultMarker);
			items = new ArrayList();
			marker = defaultMarker;
		}

		@Override
		protected OverlayItem createItem(int index) {
			return (OverlayItem) items.get(index);
		}

		@Override
		public int size() {
			return items.size();
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		public void addItem(OverlayItem item) {
			items.add(item);
			populate();
		}

		@Override
		protected boolean onTap(int index) {
			Log.d("d","Zabrana"+index);
//			Intent i = new Intent(ReportsMapActivity.this, PointActivity.class);
//			i.putExtra("title", zabrana.get(index).getTitle());
//			i.putExtra("description", zabrana.get(index).getDescription());
//			i.putExtra("image", zabrana.get(index).getImage());
//			startActivity(i);
			return true;
		}
	}
}
